package cn.csc.module.product.mq.producer.stocklock;

import cn.csc.framework.mq.core.bus.AbstractBusProducer;
import cn.csc.module.product.mq.message.stocklock.SkuStockLockMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

/**
 * 库存锁定信息 Producer
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/25 16:48
 * @modify
 */
@Slf4j
@Component
public class SkuStockLockProducer extends AbstractBusProducer {

    @Resource
    private StreamBridge streamBridge;

    // 延迟消息 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h (1-18)
    //  1级为1s，依次类推

    /**
     * 发送 {@link SkuStockLockMessage} 消息
     *
     * @param orderIds 订单id
     */
    public Boolean sendSkuStockLockMessage(List<BigInteger> orderIds) {
        SkuStockLockMessage lockMessage = new SkuStockLockMessage().setOrderIds(orderIds);

        Message<SkuStockLockMessage> message = MessageBuilder.withPayload(lockMessage)
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, 16)  // 延迟30min
                .build();
        return streamBridge.send("stockUnlock-out-0", message);
    }


}
